package com.github.raffle.atomic;

import java.util.concurrent.CountDownLatch;

/**
 * @author Stephen Liu
 * @version 1.0.0
 * @since 2017/5/24
 */
public abstract class BaseHealthChecker implements Runnable {
    private CountDownLatch latch;
    private String serviceName;
    private boolean serviceUp;


    public BaseHealthChecker(CountDownLatch latch, String serviceName) {
        this.latch = latch;
        this.serviceName = serviceName;
    }


    public String getServiceName() {
        return serviceName;
    }

    public boolean isServiceUp() {
        return serviceUp;
    }

    @Override
    public void run() {

        try {
            verifyService();
            serviceUp = true;
        }
        catch (Throwable t) {
            serviceUp = false;
        }
        finally {
            if (latch != null) {
                latch.countDown();
            }
        }
    }

    public abstract void verifyService();
}
